package oop.core.item;
import oop.core.entity.Occupation;

/**
 * 
 * @author Hoang Le
 * This represents the Equipment of the Entity
 * 
 */
public class Equipment 
{
	private static final int EQUIPMENTS_SIZE = 5;
	private EquippableItem equipments[];
	
	public Equipment()
	{
		equipments = new EquippableItem[EQUIPMENTS_SIZE];
	}
	
	public Equipment(EquippableItem[] equipments)
	{
		this.equipments = equipments;
	}
	
	/**
	 * This equip an item and return a previous item on that slot
	 * otherwise it will return null
	 * @param item
	 * @return ret
	 */
	public void equipItem(EquippableItem item)
	{
		int position = item.getItemPosition().getPosition();
		//EquippableItem ret = equipments[position];
		equipments[position] = item;
		//return ret;
		
	}
	/**
	 * Get the weapon from equipment
	 * @return
	 */
	
	public EquippableItem getWeapon()
	{
		return equipments[4];
	}
	
	/**
	 * 
	 * This method will be the one that gets called by the Mediator(using the Avatar)
	 * It first checks to see if the item is equippable for the occupation
	 * If it is, it works the same way as the method above
	 * If not, it returns the original item so that we can put it back in the Inventory
	 * @param item
	 * @param o
	 * @return
	 */
	
	public boolean equipItem(EquippableItem item, Occupation o)
	{
		if(canEquip(item,o))
		{
			equipItem(item);
			return true;
		}
		else
		{
			
			System.out.println("Not equipping, not able to");
			return false;
			//Don't equip item
		}
	}
	
	/**
	 * Check to see if an item is equippable for a certain occupation
	 * @param item
	 * @param o
	 * @return
	 */
	
	public boolean canEquip(EquippableItem item, Occupation o)
	{
		return item.hasOccupation(o) && slotAvailable(item.getItemPosition());
	}
	public boolean slotAvailable(EquippableItemPosition position)
	{	
		if(equipments[position.getPosition()] == null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	/**
	 * unequip an item passing in an index
	 * 
	 */
	public EquippableItem unEquip(int index)
	{
		EquippableItem item = equipments[index];
		remove(index);
		return item;
	}
	
	/**
	 * This is used to EMPTY out the equipment
	 * Avatar should not be able call this
	 * @param index
	 */
	private void remove(int index)
	{
		equipments[index] = null;
	}
	
	
	/**
	 * Get an item from the equipment rather than remove it
	 * @param index
	 * @return
	 */
	public EquippableItem getItem(int index)
	{
		return equipments[index];
	}
	
	
	
}